#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QWebFrame>
#include <QPrintDialog>
#include <QPrinter>

#include "../controller/AbstractState.h"
#include "../controller/Context.h"
#include "../JSBridge.h"
#include "../model/Model.h"
#include "AddDialog.h"

//--------------------------------------------------------------------------------
MainWindow::MainWindow(Context *context, JSBridge *bridge) :
    QMainWindow(),
    ui(new Ui::MainWindow),
    _context(context),
    _bridge(bridge)
{
    ui->setupUi(this);

    connect(ui->webView->page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()),
            this, SLOT(populateJavaScriptWindowObject()));

    connect(ui->actionPrint, SIGNAL(triggered()), this, SLOT(onPrintClicked()));
    connect(ui->actionSave, SIGNAL(triggered()), _context->getModel(), SLOT(save()));

    //ui->webView->page()->mainFrame()->evaluateJavaScript(QString("alert()"));
    //connect(ui->actionAdd_point, SIGNAL(triggered()), this, SLOT(onMapReady()));
}

//--------------------------------------------------------------------------------
MainWindow::~MainWindow()
{
    delete ui;
}

//--------------------------------------------------------------------------------
void MainWindow::populateJavaScriptWindowObject()
{
    qDebug("webinterface");
    ui->webView->page()->mainFrame()->addToJavaScriptWindowObject("webinterface", _bridge);
}

//--------------------------------------------------------------------------------
void MainWindow::onMapReady()
{
    ui->webView->page()->mainFrame()->evaluateJavaScript(QString("addPoint(48.35, 39.20)"));
}

//--------------------------------------------------------------------------------
void MainWindow::addNewState(AbstractState *state)
{

    QAction* newAction = new QAction(QIcon(":" + state->getImageName()), state->getName(), this);
    newAction->setCheckable(true);

    state->setAction(newAction);

    ui->mainToolBar->addAction(newAction);
}
//--------------------------------------------------------------------------------
void MainWindow::addPointToMap(float x, float y)
{
    AddDialog addDialog;
    if (addDialog.exec() == QDialog::Accepted)
    {
        ui->webView->page()->mainFrame()->evaluateJavaScript(QString("addPoint(%1, %2)").arg(x).arg(y));
        ui->listWidget->addItem(new QListWidgetItem(QIcon(":/markers/img/blue_basket.png"),
                                                    addDialog.getData().getTitle()));
    }
}

//--------------------------------------------------------------------------------
void MainWindow::onPrintClicked()
{
    QPrinter* printer = new QPrinter();
    QPrintDialog printDialog(printer, this);
    if (printDialog.exec() == QDialog::Accepted)
    {
        ui->webView->print(printer);
    }
}

//--------------------------------------------------------------------------------
void MainWindow::onSaveClicked()
{
}
//--------------------------------------------------------------------------------
